#!/usr/bin/env python
#coding=utf-8

import sys
import wx
import  wx.lib.mixins.listctrl  as  listmix
import  wx.lib.printout as  printout
import images

from panel import Panel

class SortedListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
    ''' Sorted list based  on mixins. '''

    def __init__(self, parent, ID, pos=wx.DefaultPosition,
                 size=wx.DefaultSize, style=0):
        wx.ListCtrl.__init__(self, parent, ID, pos, size, style)
        listmix.ListCtrlAutoWidthMixin.__init__(self)


class EditPanel(Panel, listmix.ColumnSorterMixin):
    ''' EditPanel for editing vocs. '''

    def __init__(self, parent, config):
        Panel.__init__(self, parent, -1, wx.DefaultPosition,
                                    wx.DefaultSize, wx.NB_TOP)

        self.create_sizer()
        self.create_searchField()
        self.create_vocLst()
        self.listSizer.Add(self.vocLst, 1, wx.EXPAND)
        self.SetSizer(self.mainSizer)
        self.SetAutoLayout(True)

    def create_searchField(self):
        ''' Search field for filtering the vocLst. '''

        searchLabel = wx.StaticText(self, label='Search')
        self.searchInput = wx.TextCtrl(self, -1,value='', size=(300,25),
            style = wx.TE_PROCESS_ENTER)
        self.printBtn = wx.BitmapButton(self, -1,
                                    wx.Bitmap('gui/icons/document-print.png'))
        self.clearBtn = wx.BitmapButton(self, -1 ,
                                        wx.Bitmap('gui/icons/editclear.png'))

        self.searchSizer.Add(searchLabel,flag=wx.TOP,border=4)
        self.searchSizer.Add(self.searchInput, flag=wx.TOP,border=0)
        self.searchSizer.Add(self.clearBtn, flag=wx.TOP, border=0)
        self.searchSizer.Add(self.printBtn, flag=wx.TOP, border=0)

    def create_sizer(self):
        ''' Probably, we will need only one sizer, but you never now. '''

        self.mainSizer = wx.BoxSizer(wx.VERTICAL)
        self.toolbarSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.searchSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.listSizer = wx.BoxSizer(wx.HORIZONTAL)

        self.mainSizer.Add(self.toolbarSizer)
        self.mainSizer.Add(self.searchSizer)
        self.mainSizer.Add(self.listSizer,1,wx.EXPAND)

    def create_vocLst(self):
        ''' Create the basic layout of our sorted list. '''

        self.imageLst = wx.ImageList(16, 16)
        self.arrowUp = self.imageLst.Add(images.getSmallUpArrowBitmap())
        self.arrowDown = self.imageLst.Add(images.getSmallDnArrowBitmap())

        self.vocLst = SortedListCtrl(self, -1,
                                         style=wx.LC_REPORT
                                         #| wx.BORDER_SUNKEN
                                         | wx.BORDER_NONE
                                         #| wx.LC_EDIT_LABELS
                                         | wx.LC_SORT_ASCENDING)
                                         #| wx.LC_NO_HEADER
                                         #| wx.LC_VRULES
                                         #| wx.LC_HRULES
                                         #| wx.LC_SINGLE_SEL)

        self.vocLst.SetImageList(self.imageLst, wx.IMAGE_LIST_SMALL)

    def GetListCtrl(self):
        ''' Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py. '''
        return self.vocLst

    def GetSortImages(self):
        return (self.arrowDown, self.arrowUp)

    def clear_search_field(self):
        '''Remove user input.'''
        self.searchInput.Clear()

    def clear_voc_lst(self):
        ''' Delete any fields from the vocLst. '''

        self.vocLst.DeleteAllItems()

    def get_search_input(self):
        ''' Return the search string. '''

        return self.searchInput.GetValue()

    def get_selected_vocId(self, idx):
        ''' Returns the vocid of the selected list item.

            The tuple was set beforehand by SetItemData(idx, key)
        '''

        return self.vocLst.GetItemData(idx)

    def populate_list(self, vocDct):
        ''' data -> (idx,(lang1,lang2,categories),...). '''

        items = vocDct.items()

        # InsertStringItem() returns an index value starting at 0.
        for key, data in items:
            idx = self.vocLst.InsertStringItem(sys.maxint, data[0])
            self.vocLst.SetStringItem(idx, 1, data[1])
            self.vocLst.SetStringItem(idx, 2, data[2])
            self.vocLst.SetItemData(idx, key)

#        self.vocLst.SetColumnWidth(0, wx.LIST_AUTOSIZE)
        self.vocLst.SetColumnWidth(0, 200)
        self.vocLst.SetColumnWidth(1, 200)
        self.vocLst.SetColumnWidth(2, 200)

        self.itemDataMap = vocDct
        listmix.ColumnSorterMixin.__init__(self, 3)

    def print_vocs(self, headline, data):
        ''' Open print preview for filtered vocs. '''

        prt = printout.PrintTable()
        prt.label = headline
        prt.set_column = []
        prt.data = data
        prt.SetFooter()
        prt.Preview()

    def ask_user(self, msg):
        '''Ask the user if he really wants to delete a voc.'''

       # msg = 'Do you really want to delete this voc?'
        dlg = wx.MessageDialog(None, msg, caption = 'Delete voc',
                            style=wx.YES_NO | wx.ICON_QUESTION,
                            pos = wx.DefaultPosition)

        result = dlg.ShowModal()
        dlg.Destroy()
        if result == wx.ID_YES:
            return True
        else:
            return False

    def set_focus(self):
        '''Set focus on the search field.'''

        wx.CallAfter(self.searchInput.SetFocus)

    def set_headline(self, headline):
            ''' Insert the lists headline. '''

            for idx, item in enumerate(headline):
                self.vocLst.InsertColumn(idx, item)

    def show_context_menu(self):
        menu = wx.Menu()
        menu.Append(wx.ID_CUT,'Delete')
        self.vocLst.PopupMenu(menu)